﻿#region  Copyright (C) 2013 Unisys Corporation

// =============================================================================================
// All Rights Reserved. This work contains trade secrets and confidential material of UNISYS, 
// and its use or disclosure in whole or in part without explicit written permission of 
// UNISYS is prohibited. 
//  
// Product Name          :  Unisys e-Workflow and Imaging 8.2
// File Name             :  Configuration.cs
// Authored By           :  Unisys Corporation
// =============================================================================================
//	Modified by                         Date            Version
// =============================================================================================
//	ronald.aguilar@cr.unisys.com      Jun 06, 2013    Initial Version
// =============================================================================================

#endregion
#region Using Region
using System;
using System.Collections.Generic;
using System.Linq;


using System.Text;
using System.Configuration;
using System.Xml;
using System.Diagnostics;

#endregion

namespace Unisys.DuplicateCheck.Entity
{
    public static  class Configuration
    {

        #region Global Resources
        public static string logLocation { get; set; }
        public static int logMaxSizeBytes { get; set; }
        public static string stringConnection { get; set; }
        public static string systemID { get; set; }
        public static string readCommand { get; set; }
        public static string updateCommand { get; set; }
        public static string duplicateRules { get; set; }
        public static string topQuery { get; set; }
        public static bool Information_Message { get; set; }
        public static bool Error_Message { get; set; }
        public static bool Log_Message { get; set; }
        public static bool Process_Message { get; set; }
        public static  int timeOutQuery { get; set; }
        #endregion

        /// <summary>
        /// Load Application Configuration
        /// </summary>
        public static void Load()
        {
            try
            {

                Configuration.logLocation = ConfigurationManager.AppSettings["logLocation"].ToString();
                
                Configuration.logMaxSizeBytes = Convert.ToInt32(ConfigurationManager.AppSettings["logMaxSizeBytes"].ToString());

                Configuration.stringConnection = ConfigurationManager.AppSettings["stringConnection"].ToString();

                Configuration.systemID = ConfigurationManager.AppSettings["systemID"].ToString();

                Configuration.readCommand = ConfigurationManager.AppSettings["readCommand"].ToString();

                Configuration.updateCommand = ConfigurationManager.AppSettings["updateCommand"].ToString();

                Configuration.duplicateRules = ConfigurationManager.AppSettings["duplicateRules"].ToString();

                Configuration.topQuery = ConfigurationManager.AppSettings["topQuery"].ToString();

                Configuration.Information_Message = Boolean.Parse(ConfigurationManager.AppSettings["Information_Message"].ToString());

                Configuration.Error_Message = Boolean.Parse(ConfigurationManager.AppSettings["Error_Message"].ToString());

                Configuration.Log_Message = Boolean.Parse(ConfigurationManager.AppSettings["Log_Message"].ToString());

                Configuration.Process_Message = Boolean.Parse(ConfigurationManager.AppSettings["Process_Message"].ToString());

                Configuration.timeOutQuery = Convert.ToInt32(ConfigurationManager.AppSettings["timeOutQuery"].ToString());
            }
            catch (Exception exc)
            {
                LogThis4Windows("Error de carga de configuracion", "Unisys Duplicate Check", "Unisys Duplicate Check");
            }

        }

        public static void SaveConfig(string logLocation,int logMaxSizeBytes, string stringConnection, string systemID, string readCommand, string updateCommand,string duplicateRules, bool Infomation_Message, bool ErrorMessage,bool Log_Message, bool Process_Message, int timeout)
        {
            
            string exePath = System.IO.Path.Combine(
                Environment.CurrentDirectory, "UnisyDuplicateCheck.exe.exe");

            System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration
                    (ConfigurationUserLevel.None);
                
           
            AppSettingsSection appS = configuration.AppSettings;

            configuration.AppSettings.Settings["logLocation"].Value = logLocation;
            configuration.AppSettings.Settings["logMaxSizeBytes"].Value = logMaxSizeBytes.ToString();
            configuration.AppSettings.Settings["stringConnection"].Value = stringConnection;
            configuration.AppSettings.Settings["readCommand"].Value = readCommand;
            configuration.AppSettings.Settings["updateCommand"].Value = updateCommand;
            configuration.AppSettings.Settings["duplicateRules"].Value = duplicateRules;
            configuration.AppSettings.Settings["Information_Message"].Value = Infomation_Message.ToString();
            configuration.AppSettings.Settings["Error_Message"].Value = ErrorMessage.ToString();
            configuration.AppSettings.Settings["Log_Message"].Value = Log_Message.ToString();
            configuration.AppSettings.Settings["Process_Message"].Value = Process_Message.ToString();
            configuration.AppSettings.Settings["systemID"].Value = systemID;
            configuration.AppSettings.Settings["timeOutQuery"].Value = timeout.ToString();
            configuration.Save(ConfigurationSaveMode.Modified);
            
        }

        public static void LogThis4Windows(string msgText, string _eventSourceName, string _eventLogName)
        {
            try
            {
                EventLog _myServiceWindowsLog = new EventLog();
                if (!EventLog.SourceExists(_eventSourceName))
                {
                    EventLog.CreateEventSource(_eventSourceName, _eventLogName);
                }
                _myServiceWindowsLog.Log = _eventLogName;
                _myServiceWindowsLog.Source = _eventSourceName;
                _myServiceWindowsLog.WriteEntry(msgText, EventLogEntryType.Error);
            }
            catch (Exception exc)
            {
            }
        }

    }
}
